package com.JL.engine.View;

import com.JL.engine.components.DimensionComponent;
import com.JL.engine.components.PositionComponent;
import com.JL.engine.model.Entity;
import com.JL.engine.model.View;

import java.awt.*;
import java.awt.geom.Point2D;

public class RectView implements View {
    private PositionComponent offsetComponent = new PositionComponent();
    private DimensionComponent dimensionComponent = new DimensionComponent();
    private Color color;
    private boolean fill;
    private int zindex;

    public RectView(int zindex,int width,int height){
        this.zindex = zindex;
        this.dimensionComponent.setSize(width,height);
    }

    public RectView(int zindex,double offsetX,double offsetY,int width,int height){
        this.zindex = zindex;
        this.dimensionComponent.setSize(width,height);
        this.offsetComponent.setPosition(offsetX,offsetY);
    }

    public RectView(int zindex,double offsetX,double offsetY,int width,int height,Color color){
        this(zindex,offsetX,offsetY,width,height);
        this.color = color;
    }

    public RectView(int zindex,int width,int height,Color color){
        this(zindex,width,height);
        this.color = color;
    }

    public RectView(int zindex,double offsetX,double offsetY,int width,int height,Color color,boolean fill){
        this(zindex,offsetX,offsetY,width,height,color);
        this.fill = fill;
    }

    public RectView(int zindex,int width,int height,Color color,boolean fill){
        this(zindex,width,height,color);
        this.fill = fill;
    }

    public void setSize(int width,int height) { this.dimensionComponent.setSize(width,height);}
    public void setOffset(double offsetX,double offsetY) {this.offsetComponent.setPosition(offsetX,offsetY);}

    public PositionComponent getOffsetComponent() {
        return offsetComponent;
    }

    public DimensionComponent getDimensionComponent() {
        return dimensionComponent;
    }

    public Color getColor() {
        return color;
    }

    public boolean isFill() {
        return fill;
    }

    public int getZindex() {
        return zindex;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void setFill(boolean fill) {
        this.fill = fill;
    }

    public void setZindex(int zindex) {
        this.zindex = zindex;
    }

    @Override
    public Point2D getOffset() {
        return offsetComponent.getPosition();
    }

    @Override
    public int getZIndex() {
        return zindex;
    }


    @Override
    public void draw(Graphics g, Entity entity) {
        Color oldColor = g.getColor();
        // 保存现场
        g.setColor(color);
        if(fill) {
            g.fillRect((int) (entity.getX()+ offsetComponent.getX()), (int) (entity.getY()+ offsetComponent.getY()), dimensionComponent.getWidth(),dimensionComponent.getHeight());
            g.drawRect((int) (entity.getX()+ offsetComponent.getX()), (int) (entity.getY()+ offsetComponent.getY()), dimensionComponent.getWidth(),dimensionComponent.getHeight());
        }
        // 还原现场
        g.setColor(oldColor);
    }
}